package cn.com.dhc.danlu.widget.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.danlu.dlsms.model.pub.res.MessageExtension;
import com.danlu.dlsms.model.pub.res.MessageExtension.OrderMessageModel.GoodsModel;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.delivery.OrderTrackActivity;
import cn.com.dhc.danlu.constant.MsgTypeConst;
import cn.com.dhc.danlu.util.CommonUtils;

/**
 * @author 吴海洋
 */
public class OrderMsgListAdapter extends MyBaseAdapter<Object> {

    public OrderMsgListAdapter(Context context) {
        super(context);
    }

    @Override
    protected View createView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.item_order_msg, parent, false);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        boolean isShowTime = true;
        final MessageExtension data = (MessageExtension) getItem(position);
        MessageExtension lastMsgModel = null;
        if (position > 0) {
            lastMsgModel = (MessageExtension) getItem(position - 1);
        }
        if (null != lastMsgModel && CommonUtils.isSameDay(lastMsgModel.getBizCreatedTimestamp(), data.getBizCreatedTimestamp())) {
            isShowTime = false;
        }
        mHolder.viewToken.setVisibility(isShowTime ? View.GONE : View.VISIBLE);
        if (isShowTime) {
            mHolder.tvTime.setText(data.getBizCreatedTimestamp().substring(0, 10));
            mHolder.tvTime.setVisibility(View.VISIBLE);
        } else {
            mHolder.tvTime.setVisibility(View.GONE);
        }
        mHolder.tvContent.setText(data.getContent());
        List<GoodsModel> goodsModelList = data.getOrderMessageModel().getGoodsModelList();
        if (!goodsModelList.isEmpty()) {
            GoodsModel model = goodsModelList.get(0);
            String imgUrl = model.getGoodsPic().contains("http") ? model.getGoodsPic() : "http:" + model.getGoodsPic();
            Uri uri = Uri.parse(imgUrl);
            mHolder.ivGoodsPic.setImageURI(uri);
        }
        final Context context = mHolder.ivGoodsPic.getContext();
        mHolder.llMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到订单跟踪界面
                String type = data.getType();
                if (type.equals(MsgTypeConst.ORDER_S_ALREADY_SHIPPED) || type.equals(MsgTypeConst.ORDER_S_CANCELLED) || type.equals(MsgTypeConst.ORDER_S_EXCEPTION)) {
                    //已发货订单(采购)  交易取消订单(采购) 交易异常订单(采购)
                    Intent intent = new Intent(context, OrderTrackActivity.class);
                    intent.putExtra("orderNo", data.getOrderMessageModel().getOrderNo());
                    context.startActivity(intent);
                }
            }
        });
        return convertView;
    }

    private static class ViewHolder {
        TextView tvTime;
        SimpleDraweeView ivGoodsPic;
        TextView tvContent;
        LinearLayout llMore;
        View viewToken;

        public ViewHolder(View view) {
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            ivGoodsPic = (SimpleDraweeView) view.findViewById(R.id.iv_goods_pic);
            tvContent = (TextView) view.findViewById(R.id.tv_content);
            llMore = (LinearLayout) view.findViewById(R.id.ll_view_more);
            viewToken = view.findViewById(R.id.view_token);
        }
    }
}